package com.sj.service;

import com.sj.dto.UserDto;
import com.sj.utils.JwtUtils;
import org.apache.shiro.crypto.hash.Sha256Hash;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * 用户信息接口
 */
@Service
public class UserService {

    private static final String encryptSalt = "F12839WhsnnEV$#23b";

//    @Autowired
//    private StringRedisTemplate redisTemplate;

    /**
     * 保存user登录信息，返回token
     */
    public String generateJwtToken(UserDto user) {
        String salt = "12345";//JwtUtils.generateSalt();
        /**
         * @todo 将salt保存到数据库或者缓存中
         * redisTemplate.opsForValue().set("token:"+username, salt, 3600, TimeUnit.SECONDS);
         */
        return JwtUtils.sign(user, salt, 3600); //生成jwt token，设置过期时间为1小时
    }

    /**
     * 获取上次token生成时的salt值和登录用户信息
     */
    public UserDto getJwtTokenInfo(String username) {
        String salt = "12345";
        /**
         * @todo 从数据库或者缓存中取出jwt token生成时用的salt
         * salt = redisTemplate.opsForValue().get("token:"+username);
         */
        UserDto user = getUserInfo(username);
        user.setSalt(salt);
        return user;
    }

    /**
     * 清除token信息
     */
    public void deleteLoginInfo(String username) {
        /**
         * @todo 删除数据库或者缓存中保存的salt
         * redisTemplate.delete("token:"+username);
         */

    }

    /**
     * 根据userName获取数据库中保存的用户信息
     */
    public UserDto getUserInfo(String userName) {
        UserDto user = new UserDto();
        user.setUserId(1L);
        user.setUsername("admin");
        user.setEncryptPwd(new Sha256Hash("123456", encryptSalt).toHex());
        List<String> roles = new ArrayList<>();
        roles.add("admin");
        user.setRoles(roles);
        return user;
    }

    /**
     * 获取用户角色列表，强烈建议从缓存中获取
     */
    public List<String> getUserRoles(Long userId) {
        return Arrays.asList("admin");
    }

    /**
     * 获取用户角色列表，强烈建议从缓存中获取
     */
    public Set<String> getUserPermissions(Long userId) {
        Set<String> set = Collections.emptySet();
        set.add("article:list");
        return set;
    }

}
